package com.zadtek.jpsqishi.receiver;

import java.util.zip.Adler32;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.MainActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import static android.content.Context.NOTIFICATION_SERVICE;

public class NotificationHelper {

    @SuppressWarnings("deprecation")
    public static void showMessageNotification(
    		Context context, NotificationManager nm, 
    		String title, String msgContent, String channel, 
    		Bundle bundle, String type) {

        boolean isChannel = !isEmpty(channel);
        String chatting = null;
        if (isChannel) {
            chatting = channel;
        } else {
            chatting = title;
        }

        Intent select = new Intent();
        select.setClass(context, MainActivity.class);
//        select.putExtra("chatting", chatting);
//        select.putExtra("isChannel", isChannel);
//        select.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        select.putExtras(bundle);
        select.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        
        Notification notification = new Notification(R.drawable.icon, msgContent, System.currentTimeMillis());
//        if (!isEmpty(channel)) {
//            title = title + " (" + channel + ")";
//        }
        
        int notificationId = getNofiticationID(title);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, select, PendingIntent.FLAG_UPDATE_CURRENT);
       // notification.setLatestEventInfo(context, title, msgContent, contentIntent);
        //*************************************
        Notification.Builder builder1 = new Notification.Builder(context);
        builder1.setSmallIcon(R.drawable.icon); //设置图标
        builder1.setTicker("显示第二个通知");
        builder1.setContentTitle(title); //设置标题
        builder1.setContentText(msgContent); //消息内容
        builder1.setWhen(System.currentTimeMillis()); //发送时间
        builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音，振动方式，灯光
        builder1.setAutoCancel(true);//打开程序后图标消失
        builder1.setContentIntent(contentIntent);
        Notification notification1 = builder1.build();
        notification1.sound = Uri.parse("android.resource://"
                + context.getPackageName() + "/" + R.raw.new_dianba_order);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(124, notification1); // 通过通知管理器发送通知
        //**************************
        showMessageNotificationLocal(context, nm, notification, notificationId, type);
    }

    private static void showMessageNotificationLocal(
    		Context context, NotificationManager nm, Notification notification, int notificationId, String type) {
        notification.ledARGB = 0xff00ff00;
        notification.ledOnMS = 300;
        notification.ledOffMS = 1000;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        
//        boolean needSound = true;
//        boolean needVibrate = true;
        
//    	if (needSound && needVibrate) {
//        	notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
//        } else if (needSound){
//        	notification.defaults = Notification.DEFAULT_SOUND;
//        } else if (needVibrate) {
//        	notification.defaults = Notification.DEFAULT_VIBRATE;
//        }
    	
    	if(type.equals("at")) {
			//设置声音,自定义声音(有新的订单)
			notification.sound = Uri.parse("android.resource://" + context.getPackageName()
					+ "/" + R.raw.new_order);
			notification.defaults = Notification.DEFAULT_VIBRATE;
		} else if(type.equals("pm")) {
			//设置声音,自定义声音(系统派单消息)
			notification.sound = Uri.parse("android.resource://" + context.getPackageName()
					+ "/" + R.raw.new_dianba_order);
			notification.defaults = Notification.DEFAULT_VIBRATE;
		} else {
			//系统默认声音
            //TODO 需要改进的地方，不清楚推送键值，直接修改默认声音，但是会默认声音和修改声音一起触发
            notification.sound = Uri.parse("android.resource://" + context.getPackageName()
                    + "/" + R.raw.new_order);
            notification.defaults = Notification.DEFAULT_VIBRATE;
//			notification.defaults = Notification.DEFAULT_SOUND;
//			notification.defaults = Notification.DEFAULT_VIBRATE;
		}

        nm.notify(notificationId, notification);
    }
    
    public static int getNofiticationID(String friend) {
        if (TextUtils.isEmpty(friend)) {
            return 0;
        }
        
        int nId = 0;
        Adler32 adler32 = new Adler32();
        adler32.update(friend.getBytes());
        nId = (int) adler32.getValue();
        if (nId < 0) {
            nId = Math.abs(nId);
        }
        
        if (nId < 0) {
            nId = Math.abs(nId);
        }
        return nId;
    }
    
    
    public static boolean isEmpty(String s) {
        if (null == s) return true;
        if (s.length() == 0) return true;
        if (s.trim().length() == 0) return true;
        return false;
    }


}
